<?php

namespace app\admin\controller;

use app\common\model\Domains;
use think\Request;

class Domain extends Base
{

    //系统日志 展示页
    public function index(Request $request)
    {
        if ( $request->isAjax() ){

            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 20);
            $map = [];
            if(session('admin')->id==1){
                
            }else{
                $map['admin'] = session('admin')->id;
            }
            $list = Domains::order(['sort'=>'desc', 'id'=>'desc'])->where($map)->page($page)->limit($limit)->select();
            foreach ($list as $key => $value){
                $value['adminname'] = db('users')->where(['id'=>$value['admin']])->value('username');
            }
                                

                  
            $data = array(
                'data'      => $list,
                'count'     => Domains::where($map)->count('id'),
                'code'      => 0,
                'msg'       => '',
            );
            return json($data);
        }
        return view();
    }


    // 添加域名
    public function create(Request $request)
    {
        if ( $request->isPost() ){
            $post = trimAll($request->post());

            $post['check']  = isset($post['check'])  ? $post['check']  : 2;
            $post['status'] = isset($post['status']) ? $post['status'] : 2;
            $post['update_time'] = time();

            // 把换行，替换成|
            $post['domain'] = str_replace(PHP_EOL, '|', $post['domain']);

            //把字符串转换成数组
            $post['domain'] = explode('|', $post['domain']);

            //整理array
            $post['domain'] = array_filter(trimAll($post['domain']));

            $list = [];
            
            foreach ($post['domain'] as $v){
                $list[] = [
                    'domain'	=> $v,
                    'admin'	=> session('admin')->id,
                    'type'		=> $post['type'],
                    'sort'		=> $post['sort'],
                    'jiekou'	=> $post['jiekou'],
                    'desc'		=> $post['desc'],
                    'check'		=> $post['check'],
                    'status'	=> $post['status'],
                'update_time'	=> $post['update_time'],
                ];
            }

            $res = new Domains;

            if ( $res->saveAll($list, false) ){
                createLog('添加域名成功');
                success('保存成功');
            }else{
                createLog('添加域名失败');
                error('保存失败');
            }

        }else {
            return view();
        }
    }

    public function edit(Request $request, $id)
    {
        if ( $request->isPost() ){
            $post = trimAll($request->post());

            $post['check']  = isset($post['check'])  ? $post['check']  : 2;
            $post['status'] = isset($post['status']) ? $post['status'] : 2;
            if(session('admin')->id==1){
                
            }else{
                $post['admin'] = session('admin')->id;
            }

            if ( Domains::where('id', $id)->update($post) ){
                createLog('编辑域名成功' . $post['domain']);
                success('保存成功');
            } else {
                createLog('编辑域名失败' . $post['domain']);
                error('保存失败');
            }

        } else {
            $data = Domains::get($id);
            $this->assign('data', $data);
            return view();
        }
    }


    public function deletedomain()
    {
        if(session('admin')->id==1){
            $datas = Domains::field('id')->where('status', 'neq', 1)->select();
        }else{
            $datas = Domains::field('id')->where('status', 'neq', 1)->where('admin', 'eq', session('admin')->id)->select();
        }
                  
        

        if ( count($datas) < 1 ){
            error('没有需要删除的域名');
        }

        $id = [];
        foreach ($datas as $value){
            $id[] = $value['id'];
        }

        if ( Domains::destroy($id) ){
            createLog('批量删除无效域名成功');
            success('操作成功');
        } else {
            error('操作失败');
        }
    }

    public function deleteall()
    {
        
        if(session('admin')->id==1){
            $datas = Domains::field('id')->select();
        }else{
            $datas = Domains::field('id')->where('admin', 'eq', session('admin')->id)->select();
        }
        
        

        if ( count($datas) < 1 ){
            error('没有需要删除的域名');
        }

        $id = [];
        foreach ($datas as $value){
            $id[] = $value['id'];
        }

        if ( Domains::destroy($id) ){
            createLog('批量删除无效域名成功');
            success('操作成功');
        } else {
            error('操作失败');
        }
    }


}
